package com.guofen.service.food.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.FoodTypeMapper;
import com.guofen.dto.FoodTypeGroupByType;
import com.guofen.pojo.FoodType;
import com.guofen.service.food.FoodTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class FoodTypeServiceImpl implements FoodTypeService {
    @Autowired
    private FoodTypeMapper mapper;

    @Override
    public Map<String, Object> getAllFood(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<FoodType> list = mapper.selectAll();
        PageInfo pageUser = new PageInfo(list);
        long sum = pageUser.getTotal();
        list = pageUser.getList();
        map.put("list",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByCost(int cost1, int cost2,
                                         int pageSize, int currentPage) {
        if (cost1 > cost2) {
            int temp = cost1;
            cost1 = cost2;
            cost2 = temp;
        }
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<FoodType> list = mapper.selectByCost(cost1, cost2);
        PageInfo<FoodType> pageUser = new PageInfo<>(list);
        long sum = pageUser.getTotal();
        list = pageUser.getList();
        map.put("list",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByCostColor(int cost1, int cost2, String color,
                                              int pageSize, int currentPage) {
        if (cost1 > cost2) {
            int temp = cost1;
            cost1 = cost2;
            cost2 = temp;
        }
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<FoodType> list = mapper.selectByCostColor(cost1, cost2, color);
        PageInfo<FoodType> pageUser = new PageInfo<>(list);
        long sum = pageUser.getTotal();
        list = pageUser.getList();
        map.put("list",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByCostType(int cost1, int cost2, String type,
                                             int pageSize, int currentPage) {
        if (cost1 > cost2) {
            int temp = cost1;
            cost1 = cost2;
            cost2 = temp;
        }
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<FoodType> list = mapper.selectByCostType(cost1, cost2, type);
        PageInfo pageUser = new PageInfo(list);
        long sum = pageUser.getTotal();
        list = pageUser.getList();
        map.put("list",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByType(String type, int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<FoodType> list = mapper.selectByType(type);
        PageInfo pageUser = new PageInfo(list);
        long sum = pageUser.getTotal();
        list = pageUser.getList();
        map.put("list",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public List<FoodTypeGroupByType> getGroupByType() {
        List<String> types = mapper.selectTypes();
        List<FoodTypeGroupByType> foods = new ArrayList<>();
        for (String type : types) {
            FoodTypeGroupByType temp = new FoodTypeGroupByType();
            temp.setType(type);
            temp.setFoods(mapper.selectByType(type).stream().map(FoodType::getName).collect(Collectors.toList()));
            foods.add(temp);
        }
        return foods;
    }

    @Override
    public List<String> getNameByType(String type) {
        List<FoodType> list = mapper.selectByType(type);
        return list.stream().map(FoodType::getName).collect(Collectors.toList());
    }

    @Override
    public Map<String, Object> getByColor(String color, int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<FoodType> list = mapper.selectByColor(color);
        PageInfo pageUser = new PageInfo(list);
        long sum = pageUser.getTotal();
        list = pageUser.getList();
        map.put("list",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTypeColor(String type, String color,
                                              int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<FoodType> list = mapper.selectByTypeColor(type, color);
        PageInfo pageUser = new PageInfo(list);
        long sum = pageUser.getTotal();
        list = pageUser.getList();
        map.put("list",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public List<Integer> getColorDistribution() {
        List<String> colors = new ArrayList<>();
        colors.add("red");      colors.add("white");    colors.add("green");
        colors.add("yellow");   colors.add("blue");
        List<Integer> list = new ArrayList<>();
        for (String color : colors) {
            list.add(mapper.selectColorCount(color));
        }
        return list;
    }

    @Override
    public List<Integer> getColorDisSelective(String labels) {
        List<Integer> list = new ArrayList<>();
        for (String color : labels.split("_")) {
            list.add(mapper.selectColorCount(color));
        }
        return list;
    }

    @Override
    public List<Integer> getTypeDistribution() {
        List<String> types = new ArrayList<>();
        types.add("fruit"); types.add("meat"); types.add("fish"); types.add("veg");
        List<Integer> list = new ArrayList<>();
        for (String type : types) {
            list.add(mapper.selectTypeCount(type));
        }
        return list;
    }

    @Override
    public Map<String, Object> getCostDistribution(int groupNum) {
        List<Integer> limits = new ArrayList<>();
        for (int i = 0; i < groupNum; i++) {
            int temp = 29+(629-29)/groupNum*(i+1);
            limits.add(mapper.selectMaxCostInLimit(temp));
        }
        List<Integer> distribution = new ArrayList<>();
        List<String> labels = new ArrayList<>();
        for (int i = 0; i < limits.size(); i++) {
            if (i == 0) {
                labels.add("0~"+limits.get(0));
                distribution.add(mapper.selectCostCount(0, limits.get(0)));
            } else {
                labels.add(limits.get(i-1)+"~"+limits.get(i));
                distribution.add(mapper.selectCostCount(limits.get(i-1), limits.get(i)));
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("label", labels);
        map.put("distribution", distribution);
        return map;
    }

    @Override
    public FoodType getByName(String name) {
        return mapper.selectByPrimaryKey(name);
    }

    @Override
    public int delete(String name) {
        return mapper.deleteByPrimaryKey(name);
    }

    @Override
    public int update(FoodType food) {
        return mapper.updateByPrimaryKeySelective(food);
    }

    @Override
    public int insert(FoodType food) {
        return mapper.insertSelective(food);
    }
}
